
vote4blog_url='http:\/\/vote4blog.appspot.com\/';
find_marks();

function find_marks(){
	var res;
	body = document.body.innerHTML;
	
	if (res = body.match(/#\{[^}]*\}/g)) {
	// Successful match
		//alert(res);
		//return res;
	} else {
		// Match attempt failed
		//alert('no cmd mark found')
		return false;
	}	
	
	
	var mark_name = 'vote4blog_mark_';
	for(i=0;res[i];i++){
		mark = res[i];
		mark_reg = mark.replace(/\//g, "\\/");
		mark_reg = mark_reg.replace(/\{/g, "\\{");
		mark_reg = mark_reg.replace(/\}/g, "\\}");
		mark_reg = mark_reg.replace(/\./g, "\\.");
		
		var reg=new RegExp(mark_reg,"g"); //创建正则RegExp对象

		//alert('mark='+mark);
		
		//alert('mark_reg='+mark_reg);
		
		body = body.replace(reg,'<div class="mark" id="' + mark_name + i + '"></div>');
		//alert(body);
		
		document.body.innerHTML = body;
		mark = mark.substr(2,mark.length-3);
		//alert('cmd='+mark);
		parse_mark(mark,mark_name+i);
	}	
}

function parse_mark(mark,back_id){
	param = mark.split(',');
	switch(param[0]){
		case 'giv_me_rank_info':
			var rank_url;
			if(param[1]){
				rank_url = param[1];
			}else{
				rank_url = get_cur_url();
			}
			//init jQuery with callback, which will rewrite HTML
			sndReqShowRank(rank_url,back_id);
			break;
		case 'giv_me_vote_info':
			alert('vote function is not released!');
			break;
	}
}

function get_cur_url(){	
		
	url = window.location.href;
	pos = url.indexOf("?");	
	if (pos>0){  
		url = url.substr(0,pos-1);
	}
	return url;
}
function myXMLHttpRequest() {
	var xmlhttp;
   
	if(window.XMLHttpRequest) { 
        //针对FireFox,Mozillar,Opera,Safari,IE7,IE8 
        xmlhttp = new XMLHttpRequest(); 
        if(xmlhttp.overrideMimeType){ 
            xmlhttp.overrideMimeType("text/xml"); 
        } 
    }
    else if(window.ActiveXObject){ 
        //针对IE6,IE5.5,IE5 
        //两个可以用于创建XMLHTTPRequest对象的控件名称,保存在一个js数组中 
        //排在前面的版本较新 
        var activexName = ["MSXML2.XMLHTTP","Microsoft.XMLHTTP"]; 
        for(var i = 0;i < activexName.length; i++){ 
            try{ 
                //取出一个控件名进行创建,如果创建成功就终止循环 
                //如果创建失败,会抛出异常,然后可以继续循环,继续尝试创建 
                xmlhttp = new ActiveXObject(activexName[i]); 
                break; 
            }catch(e){ 
            } 
        } 
    }  
	
	if(!xmlhttp && typeof XMLHttpRequest != "undefined")
		xmlhttp = new XMLHttpRequest();
	if (!xmlhttp)
		alert("Sigh~~~你用的啥浏览器啊，告诉俺吧");
	return xmlhttp;
}
//rank_url	-- key of rank page , should be the url
//back_pos	-- id of rewrite div, which callback need to know
function sndReqShowRank(rank_url,back_pos)
{
	tmp_url_string = vote4blog_url + 'read';
	$(document).ready(
		function(){
		$.getJSON(tmp_url_string+'?callback=?',{url:rank_url,back_pos:back_pos},handleShowRating);
		}
	);
  
}

function handleShowRating(data) {
	
	//incoming data
	var mark = data.back_pos;
	var res = data.res;
	var rank = new Number(data.rank);
	var count = data.count;
	var ranked_url = data.url;
	
	var star_size = 30;
	var solid_width = (count <= 0)?'0px':rank*star_size + 'px';
	var vote4blog_image_star = 'http://vote4blog.appspot.com/image/star.gif';
	//calc padding
	var rank_info_text = (count <= 0)?'<div id=\"Rating-no\">请猛击小星星，给作者投票，不限次数hiahia</div>':'<div id=\"Rating-show\">平均得分' + rank.toFixed(2) + ' ，共' + count + '票</div>';
	var hollow_style = 'background-image:url(' + vote4blog_image_star + ');float:left;height:'+star_size+'px;width:'+star_size*5+'px;';
	var solid_style = 'background-image:url(' + vote4blog_image_star + ');background-position:0 -60px;height:'+star_size+'px;position:absolute;width:'+solid_width+';z-index:11';

	document.getElementById(mark).innerHTML = 
	"<div class=\"Rating\" id=\""+mark+'_Rating'+"\">\
		<div class=\"StarHollow\" id=\""+mark+'_StarHollow'+"\" style=\""+ hollow_style+"\">\
			<div class=\"StarSolid\" id=\""+mark+'_StarSolid'+"\" style=\""+ solid_style +"\"><\/div>\
			<a href=\"javascript:Rating(\'"+mark+'\',\''+ranked_url+"\',1);\" id=\"Star1\" title=\"太烂了！\"><\/a>\
			<a href=\"javascript:Rating(\'"+mark+'\',\''+ranked_url+"\',2);\" id=\"Star2\" title=\"不咋地，凑乎吧~\"><\/a>\
			<a href=\"javascript:Rating(\'"+mark+'\',\''+ranked_url+"\',3);\" id=\"Star3\" title=\"还算是个文章~\"><\/a>\
			<a href=\"javascript:Rating(\'"+mark+'\',\''+ranked_url+"\',4);\" id=\"Star4\" title=\"这真的是你写的？\"><\/a>\
			<a href=\"javascript:Rating(\'"+mark+'\',\''+ranked_url+"\',5);\" id=\"Star5\" title=\"哇,太精彩了~~你是我的呕像！\"><\/a>\
		<\/div>\
		<div class=\"Score\" id=\""+ mark + '_Score'+"\">"+rank_info_text+"<\/div>\
	<\/div>";
}

function Rating(eid,url,rank)
{
	document.getElementById(eid+'_Score').innerHTML = "<div id=\"Rating-now\">正在为您投出神圣的一票...</div>";

	tmp_url_string = vote4blog_url + 'write';
	$(document).ready(
		function(){
		$.getJSON(tmp_url_string+'?callback=?',{url:url,rank:rank,back_pos:eid},handleRating);
		}
	);	
	
//	xmlhttp=myXMLHttpRequest();
//    xmlhttp.open('get', url_rank + 'write?url=' + url + '&score=' + rank);
//    xmlhttp.onreadystatechange = handleRating;
//    xmlhttp.send(null);	
}

function handleRating(data) {
	var mark = data.back_pos;
	var res = data.res;
	var avg = new Number(data.rank);
	var count = data.count;
	
	var star_size = 30;
	var solid_width = (count <= 0)?'0px':avg*star_size + 'px';
	var vote4blog_image_star = 'http://vote4blog.appspot.com/image/star.gif';
		
	if (res == "succ"){
		avg = avg.toFixed(2);		
		
		document.getElementById(mark + "_StarSolid").style.width = solid_width;	
		document.getElementById(mark + "_Score").innerHTML = "<div id=\"Rating-Tanks\">投票成功，感谢您的使用</div>";
		document.getElementById(mark + "_Score").innerHTML = "<div id=\"Rating-show\">平均得分" + avg + "，共有" + count + "票</div>";
	}else if (res == "dup"){
		document.getElementById(mark + "_Score").innerHTML = "<div id=\"Rating-already\">您已经为此文章投过一票啦</div>";
	}else if (Rdata=="err"){
		document.getElementById(mark + "_Score").innerHTML = "<div id=\"Rating-err\">汗，出错了</div>";
	}else{
		document.getElementById(mark + "_Score").innerHTML = "<div id=\"Rating-err\">汗，又出错了</div>";
	}
}
